<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料编码" prop="cInvCode">
        <el-select v-model="queryParams.cInvCode" clearable filterable
                   @keyup.enter.native="handleQuery"
        >
          <el-option v-for="item in nccOptionsList" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="药品子类编码" prop="typeNo" label-width="100px">
        <el-select v-model="queryParams.typeNo" clearable filterable
                   @keyup.enter.native="handleQuery"
        >
          <el-option v-for="item in msfxOptions"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['association:association:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['association:association:edit']"-->
      <!--        >修改-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['association:association:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['association:association:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="associationList" @selection-change="handleSelectionChange"
              border
              style="width: 100%; margin-top: 20px"
    >
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-descriptions title="码上放心药品信息" style="margin-left: 20px">
            <el-descriptions-item label="药品名称">{{ props.row.productName }}</el-descriptions-item>
            <el-descriptions-item label="制剂规格">{{ props.row.spec }}</el-descriptions-item>
            <el-descriptions-item label="包装规格">{{ props.row.packageSpec }}</el-descriptions-item>
            <el-descriptions-item label="包装单位">{{ props.row.packUnit }}</el-descriptions-item>
            <el-descriptions-item label="批准文号">{{ props.row.authorizedNo }}</el-descriptions-item>
          </el-descriptions>
          <el-descriptions title="NCC药品信息" style="margin-left: 20px">
            <el-descriptions-item label="药品名称">{{ props.row.DrugName }}</el-descriptions-item>
            <el-descriptions-item label="药品规格">{{ props.row.DrugGG }}</el-descriptions-item>
            <el-descriptions-item label="包装规格">{{ props.row.DrugPkg }}</el-descriptions-item>
            <el-descriptions-item label="包装比例">{{ props.row.DrugPackNum }}</el-descriptions-item>
            <el-descriptions-item label="批准文号">{{ props.row.DrugApprovalSN }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>
      <el-table-column label="数据编号" align="center" prop="ID"/>
      <el-table-column label="药品子类编码" align="center" prop="typeNo"/>
      <el-table-column label="物料编码" align="center" prop="DrugEntID"/>
      <el-table-column label="换算系数" align="center" prop="unitNum"/>
      <el-table-column label="重点药品" align="center" prop="keynote">
        <template slot-scope="scope">
          <el-tag :type="scope.row.keynote === '1' ? 'danger' : ''">
            {{ scope.row.keynote === '1' ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="memo"/>
      <el-table-column label="操作" align="center" width="120px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['association:association:edit']"
          >修改
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['association:association:remove']"-->
          <!--          >删除-->
          <!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加数据关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :label-position="'top'">
        <el-card class="box-card" style="margin-bottom: 10px;">
          <!--          <el-row>-->
          <!--            <el-form-item label="工厂信息" prop="factoryId" v-hasRole="['spurAdmin']">-->
          <!--              <el-select v-model="form.factoryId" filterable-->
          <!--                         @change="getDrugInfo">-->
          <!--                <el-option v-for="item in factoryOptions"-->
          <!--                           :key="item.value"-->
          <!--                           :label="item.label"-->
          <!--                           :value="item.value"-->
          <!--                />-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-row>-->
          <div slot="header" class="clearfix">
            <span>码上放心药品编码信息</span>
          </div>
          <el-row>
            <el-col :span="8">
              <el-form-item label="药品子类编码" prop="typeNo">
                <el-input v-model="queryTypeNo" @input="queryTypeNoInfo" placeholder="请输入追溯码前七位"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="药品名称" prop="drugName">
                <el-input style="width: 220px" v-model="drugInfo.drugName" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="制剂规格" prop="spec">
                <el-input style="width: 220px" v-model="drugInfo.spec" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="包装规格" prop="pkgSpec">
                <el-input style="width: 220px" v-model="drugInfo.pkgSpec" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="批准文号" prop="acceptNo">
                <el-input style="width: 220px" v-model="drugInfo.acceptNo" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="包装单位" prop="unit">
                <el-input style="width: 220px" v-model="drugInfo.unit" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="memo1">
                <el-input style="width: 220px" v-model="drugInfo.memo" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
        <el-card class="box-card" style="margin-bottom: 10px;">
          <div slot="header" class="clearfix">
            <span>NCC药品编码信息</span>
          </div>
          <el-row>
            <el-form-item label="NCC药品编码" prop="cInvCode">
              <el-select v-model="form.cInvCode" clearable filterable
                         @change="showNcc"
              >
                <el-option v-for="item in nccOptions"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="药品名称" prop="DrugName">
                <el-input style="width: 220px" v-model="nccInfo.DrugName" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="药品规格" prop="DrugGG">
                <el-input style="width: 220px" v-model="nccInfo.DrugGG" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="包装规格" prop="DrugPkg">
                <el-input style="width: 220px" v-model="nccInfo.DrugPkg" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="批准文号" prop="DrugApprovalSN">
                <el-input style="width: 220px" v-model="nccInfo.DrugApprovalSN" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="包装比例" prop="DrugPackNum">
                <el-input style="width: 220px" v-model="nccInfo.DrugPackNum" readonly/>
              </el-form-item>
            </el-col>
            <!--            <el-col :span="8">-->
            <!--              <el-form-item label="备注" prop="pkgSpec">-->
            <!--                <el-input style="width: 220px" v-model="nccInfo.memo" readonly/>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
          </el-row>
        </el-card>

        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>信息补充</span>
          </div>
          <el-row>
            <el-col :span="8">
              <el-form-item label="换算系数" prop="unitNum">
                <el-input style="width: 220px" v-model.number="form.unitNum" placeholder="请输入换算系数"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="重点监管药品" prop="keynote">
                <el-select v-model="form.keynote" clearable>
                  <el-option key="0" label="否" value="0"/>
                  <el-option key="1" label="是" value="1"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="Memo">
                <el-input style="width: 220px" v-model="form.Memo" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  修改弹窗  -->
    <el-dialog :title="title" :visible.sync="updateOpen" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :label-position="'top'">
        <el-card class="box-card" style="margin-bottom: 10px;">
          <div slot="header" class="clearfix">
            <span>码上放心药品编码信息</span>
          </div>
          <el-row>
            <el-form-item label="药品子类编码" prop="typeNo">
              <el-select v-model="form.typeNo" clearable
                         @change="showMsfx" disabled
              >
                <el-option v-for="item in msfxOptions"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="药品名称" prop="drugName">
                <el-input style="width: 220px" v-model="drugInfo.drugName" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="制剂规格" prop="spec">
                <el-input style="width: 220px" v-model="drugInfo.spec" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="包装规格" prop="pkgSpec">
                <el-input style="width: 220px" v-model="drugInfo.pkgSpec" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="批准文号" prop="acceptNo">
                <el-input style="width: 220px" v-model="drugInfo.acceptNo" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="包装单位" prop="unit">
                <el-input style="width: 220px" v-model="drugInfo.unit" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="memo1">
                <el-input style="width: 220px" v-model="drugInfo.memo" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
        <el-card class="box-card" style="margin-bottom: 10px;">
          <div slot="header" class="clearfix">
            <span>NCC药品编码信息</span>
          </div>
          <el-row>
            <el-form-item label="NCC药品编码" prop="cInvCode">
              <el-select v-model="form.cInvCode" clearable
                         @change="showNcc"
              >
                <el-option v-for="item in nccOptions"
                           :key="item.value"
                           :label="item.label"
                           :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-row>

          <el-row>
            <el-col :span="8">
              <el-form-item label="药品名称" prop="DrugName">
                <el-input style="width: 220px" v-model="nccInfo.DrugName" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="药品规格" prop="DrugGG">
                <el-input style="width: 220px" v-model="nccInfo.DrugGG" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="包装规格" prop="DrugPkg">
                <el-input style="width: 220px" v-model="nccInfo.DrugPkg" readonly/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="批准文号" prop="DrugApprovalSN">
                <el-input style="width: 220px" v-model="nccInfo.DrugApprovalSN" readonly/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="包装比例" prop="DrugPackNum">
                <el-input style="width: 220px" v-model="nccInfo.DrugPackNum" readonly/>
              </el-form-item>
            </el-col>
            <!--            <el-col :span="8">-->
            <!--              <el-form-item label="备注" prop="pkgSpec">-->
            <!--                <el-input style="width: 220px" v-model="nccInfo.memo" readonly/>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
          </el-row>
        </el-card>

        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>信息补充</span>
          </div>
          <el-row>
            <el-col :span="8">
              <el-form-item label="换算系数" prop="unitNum">
                <el-input style="width: 220px" v-model.number="form.unitNum" placeholder="请输入换算系数"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="重点监管药品" prop="keynote">
                <el-select v-model="form.keynote" clearable>
                  <el-option key="0" label="否" value="0"/>
                  <el-option key="1" label="是" value="1"/>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="备注" prop="Memo">
                <el-input style="width: 220px" v-model="form.Memo" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  insertDrugMap,
  listMsfx,
  listNcc,
  listDrugMap,
  delDrugMap,
  getDrugMap,
  updateDrugMap,
  queryErpDrug,
  selectTypeNoByDrugCode
} from '@/api/msfx/mapping/drugMap/drugMap'

export default {
  name: 'drugMap',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      queryTypeNo: '',
      // 数据关联表格数据
      associationList: [],
      //NCC下拉框
      nccOptions: [],
      //查询用ncc下拉框
      nccOptionsList: [],
      //码上放心下拉框
      msfxOptions: [],
      //工厂信息
      factoryOptions: [],
      //NCC药品信息
      nccInfo: {
        DrugEntID: null,
        DrugName: null,
        DrugPkg: null,
        DrugPackNum: null,
        DrugApprovalSN: null,
        DrugGG: null
      },
      //码上放心药品信息
      drugInfo: {
        drugName: null,
        spec: null,
        pkgSpec: null,
        acceptNo: null,
        unit: null,
        memo: null
      },
      type: null,
      //存ncc查询到的药品数据
      nccList: [],
      // 存码上放心查询到的药品数据
      msfxList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      updateOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cInvCode: null,
        UnitNum: null,
        typeNo: null,
        Memo: null,
        Memo1: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeNo: [
          { required: true, message: '药品子类编码不能为空', trigger: 'blur' }
        ],
        cInvCode: [
          { required: true, message: 'NCC药品编码不能为空', trigger: 'blur' }
        ],
        keynote: [
          { required: true, message: '是否重点监管药品不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDrugInfo()
    this.getERPDrug()
  },
  methods: {
    /** 查询数据关联列表 */
    getList() {
      this.loading = true
      listDrugMap(this.queryParams).then(response => {
        this.associationList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 获取码上放心药品信息 */
    getDrugInfo() {
      this.drugInfo = {
        drugName: null,
        spec: null,
        pkgSpec: null,
        acceptNo: null,
        unit: null,
        memo: null
      }
      this.nccInfo = {
        DrugEntID: null,
        DrugName: null,
        DrugPkg: null,
        DrugPackNum: null,
        DrugApprovalSN: null,
        DrugGG: null
      }
      this.form.typeNo = null
      this.form.cInvCode = null
      listMsfx(this.form.factoryId).then(response => {
        this.msfxOptions = []
        this.msfxList = []
        this.msfxList = response.data
        this.msfxList.forEach(item => {
          this.msfxOptions.push({
            label: item.typeNo + ' ' + item.productName,
            value: item.typeNo
          })
        })
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.updateOpen = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        typeNo: null,
        cInvCode: null,
        unitNum: null,
        Memo: null,
        Memo1: null,
        keynote: null,
        id: null
      }
      this.queryTypeNo = ''
      this.drugInfo = {
        drugName: null,
        spec: null,
        pkgSpec: null,
        acceptNo: null,
        unit: null,
        memo: null
      }
      this.nccInfo = {
        DrugEntID: null,
        DrugName: null,
        DrugPkg: null,
        DrugPackNum: null,
        DrugApprovalSN: null,
        DrugGG: null
      }
      this.resetForm('form')
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.type = 1
      this.open = true
      this.title = '新增药品信息映射'
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.type = 0
      const ids = row.ID || this.ids
      getDrugMap(ids).then(response => {
        this.form.typeNo = response.data.typeNo
        this.form.cInvCode = response.data.DrugEntID
        this.nccInfo.DrugName = response.data.DrugName
        this.nccInfo.DrugApprovalSN = response.data.DrugApprovalSN
        this.nccInfo.DrugPackNum = response.data.DrugPackNum
        this.nccInfo.DrugPkg = response.data.DrugPkg
        this.nccInfo.DrugGG = response.data.DrugGG
        this.form.unitNum = response.data.unitNum
        this.form.keynote = response.data.keynote
        this.form.id = ids
        this.showMsfx()

        this.updateOpen = true
        this.title = '修改药品信息映射'
      })
    },

    /** 新增提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.type === 0) {
            updateDrugMap(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.updateOpen = false
              this.getList()
            })
          } else if (this.type === 1) {
            insertDrugMap(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const typeNos = row.ID || this.ids
      this.$modal.confirm('是否确认删除数据关联编号为"' + typeNos + '"的数据项？').then(function() {
        return delDrugMap(typeNos)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('association/association/export', {
        ...this.queryParams
      }, `association_${new Date().getTime()}.xlsx`)
    },

    /**
     * ncc药品详细信息
     */
    showNcc() {
      this.nccList.forEach(item => {
        if (item.DrugEntID === this.form.cInvCode) {
          this.nccInfo.DrugName = item.DrugName
          this.nccInfo.DrugApprovalSN = item.DrugApprovalSN
          this.nccInfo.DrugPackNum = item.DrugPackNum
          this.nccInfo.DrugPkg = item.DrugPkg
          this.nccInfo.DrugGG = item.DrugGG
        }
      })
    },

    /**
     * 码上放心药品详细信息
     */
    showMsfx() {
      this.msfxList.forEach(item => {
        if (item.typeNo === this.form.typeNo) {
          this.drugInfo.unit = item.packUnit
          this.drugInfo.drugName = item.productName
          this.drugInfo.acceptNo = item.authorizedNo
          this.drugInfo.pkgSpec = item.packageSpec
          this.drugInfo.spec = item.spec
        }
      })

      listNcc(this.drugInfo.drugName).then(response => {
        //清空列表
        this.nccList = []
        this.nccOptions = []
        if (this.type !== 0) {
          this.nccInfo = {}
          this.form.cInvCode = null
        }
        //组装数据
        this.nccList = response.data
        this.nccList.forEach(item => {
          this.nccOptions.push({
            label: item.DrugEntID + ' ' + item.DrugName,
            value: item.DrugEntID
          })
        })
      })
    },

    // //查询工厂信息
    // getFactoryList() {
    //   Fusion_Manufacturer_Info().then(response => {
    //     this.factoryOptions = response.data;
    //   });
    // },

    //查询ERP药品信息
    getERPDrug() {
      queryErpDrug().then(response => {
        response.data.forEach(item => {
          this.nccOptionsList.push({
            label: item.DrugEntID + ' ' + item.DrugName,
            value: item.DrugEntID
          })
        })
      })
    },

    /** 根据药品追溯码前七位查询码上放心药品信息*/
    queryTypeNoInfo() {
      // 1. 统一初始化表单和数据对象
      this.resetFormData();

      // 2. 仅当条件满足时执行查询
      if (this.queryTypeNo?.length >= 7) {
        this.fetchTypeNoAndDrugInfo();
      }
    },

// 初始化所有表单和数据对象
    resetFormData() {
      this.form = {
        typeNo: '',
        cInvCode: null,
        unitNum: null,
        Memo: null,
        Memo1: null,
        id: null
      };
      this.drugInfo = {
        drugName: null,
        spec: null,
        pkgSpec: null,
        acceptNo: null,
        unit: null,
        memo: null
      };
      this.nccInfo = {
        DrugEntID: null,
        DrugName: null,
        DrugPkg: null,
        DrugPackNum: null,
        DrugApprovalSN: null,
        DrugGG: null
      };
      this.nccList = [];
      this.nccOptions = [];
    },

// 异步获取药品信息和NCC数据
    async fetchTypeNoAndDrugInfo() {
      try {
        const typeNoPrefix = this.queryTypeNo.substring(0, 7);
        const typeNoResponse = await selectTypeNoByDrugCode(typeNoPrefix);

        // 更新药品基础信息
        this.updateDrugInfo(typeNoResponse.data);

        // 获取并处理NCC数据
        if (this.type !== 0) {
          await this.fetchAndProcessNccData();
        }
      } catch (error) {
        console.error("获取药品信息失败:", error);
      }
    },

// 更新药品信息
    updateDrugInfo(data) {
      if (!data) return;

      this.form.typeNo = data.typeNo;
      this.drugInfo = {
        ...this.drugInfo,
        unit: data.packUnit,
        drugName: data.productName,
        acceptNo: data.authorizedNo,
        pkgSpec: data.packageSpec,
        spec: data.spec
      };
    },

// 获取并处理NCC数据
    async fetchAndProcessNccData() {
      try {
        // 强制清空（确保响应式）
        this.$set(this, 'nccList', []);
        this.$set(this, 'nccOptions', []);
        if (this.drugInfo.drugName == null || this.drugInfo.drugName.length === 0){
          return;
        }
        // 请求数据
        const nccResponse = await listNcc(this.drugInfo.drugName);

        // 更新数据
        this.$set(this, 'nccList', nccResponse.data || []);
        this.nccOptions = this.nccList.map(item => ({
          label: `${item.DrugEntID} ${item.DrugName}`,
          value: item.DrugEntID
        }));

        // 重置NCC信息
        if (this.type !== 0) {
          this.nccInfo = {};
          this.form.cInvCode = null;
        }
      } catch (error) {
        console.error("获取NCC数据失败:", error);
      }
    }
  }
}
</script>
